from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import urllib


#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response, keyword):
    allData = []

    result = json.loads(response.json())
    allNum = result["data"]["total"]
    resList = result["data"]["result"]
    # print(type(json.loads(lis)))
    # print(lis["data"]['result'])
    for li in resList:
        tempData = resultData()
        try:
            tempData["titleName"] = li["title"]
        except:
            pass
        try:
            tempData["titleHref"] = "/reader/jpath/qwreader?ssid=" + li["ssid"] + "&dxid=" + li["dxid"] + "&d=" + li["jpathD"] + "&sKey=" + keyword + "&pagenum=65&pd=" + li["qwPageD"]
        except: 
            pass
        try:
            tempData['abstract'] = li['content']
        except:
            pass

        allData.append(tempData)

    return allData, allNum

def getData(keyword):
    headers = {
        'Origin': 'http://www.sslibrary.com',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en-CA;q=0.7,en;q=0.6',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        'Proxy-Authorization': 'Basic cWY6cWYzLjE0MTU=',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Referer': 'http://www.sslibrary.com/book/search?sw='+ keyword +'^&allsw=^&bCon=^&field=all',
        'X-Requested-With': 'XMLHttpRequest',
        'Proxy-Connection': 'keep-alive',
    }

    data = {
        'sw': keyword,
        'allsw': '',
        'searchtype': '',
        'classifyId': '',
        'isort': '',
        'field': 'all',
        'jsonp': '',
        'showcata': '',
        'expertsw': '',
        'bCon': '',
        'page': '1',
        'pagesize': '10',
        'sign': '',
        'enc': ''
    }

    proxies = {
        "http": "http://qf:qf3.1415@202.112.118.7:10086/"
    }
    

    response = requests.post('http://www.sslibrary.com/book/search/qw', headers=headers, data=data,proxies = proxies)


    allData, allNum = formData(response, keyword)


    return allData, allNum


def index(request):


    # page = 1
    data = json.loads(request.POST.get('data', '1'))

    allData, allNum = getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")